package com.maserb.monitor.service.jwOur.impl;

import com.maserb.monitor.entity.JwEducationClobEntity;
import com.maserb.monitor.entity.JwEducationInfoEntity;
import com.maserb.monitor.entity.jwOur.JwOurEntity;
import com.maserb.monitor.repository.JwEducationClobRepository;
import com.maserb.monitor.repository.jwOur.JwOurRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwOur.JwOurService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class JwOurServiceImpl extends JpaSpecificationServiceImpl<JwOurRepository,
        JwOurRepository, JwOurEntity, String>
        implements JwOurService {

    @Autowired
    private JwEducationClobRepository clobRepository;

    @Override
    public List<JwOurEntity> findByType(String type) {
        return this.repository.findByType(type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(JwOurEntity entity, JwEducationClobEntity clobEntity) {
        if (entity.getId() != null) {
            JwEducationClobEntity entityClob = this.findOne(entity.getId()).getClob();
            clobRepository.delete(entityClob);
        }
        clobRepository.save(clobEntity);
        this.save(entity);
    }
}